package com.booth.base.controllers;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import com.booth.base.model.Users;
import com.booth.base.utils.ResultStatus;
import com.booth.base.utils.UploadUtils;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.render.JsonRender;
import com.jfinal.upload.UploadFile;

public class BaseController extends Controller {
	//admin
	public static final int GROUP_TYPE_1 = 1;
	//城管
	public static final int GROUP_TYPE_2 = 2;
	//经营户
	public static final int GROUP_TYPE_3 = 3;
	
	protected void renderResult(ResultStatus result) {
		setAttr("success", Integer.valueOf(result.getCode()));
		setAttr("msg", result.getMsg());
		render(new JsonRender().forIE());
	}

	protected void renderResult(Integer code, String msg) {
		setAttr("success", code);
		setAttr("msg", msg);
		render(new JsonRender().forIE());
	}

	protected Integer getLoginUserId() {
		HttpSession session = getSession();
		Users obj = (Users) session.getAttribute("user");
		return obj.getInt("user_id");
	}

	protected String getLoginUserName() {
		HttpSession session = getSession();
		Users obj = (Users) session.getAttribute("user");
		return obj == null ? "" : obj.getStr("user_name");
	}

	protected Users getCurUser() {
		HttpSession session = getSession();
		Users obj = (Users) session.getAttribute("user");
		return obj;
	}

	protected Integer getGroupId() {
		HttpSession session = getSession();
		Users obj = (Users) session.getAttribute("user");
		return obj.getInt("group_id");
	}

	protected Integer getDepartmentId() {
		HttpSession session = getSession();
		Users obj = (Users) session.getAttribute("user");
		return obj.getInt("dep_id");
	}

	protected Model setCreator(Model model, Users u) {
		if(u != null){
			model.set("creator_id", u.getUserId());
			model.set("creator_name", u.getUserName());
		}
	
		return model;
	}

	protected Model setOperator(Model model, Users u) {
		model.set("update_time", new Date());
		model.set("updator_id", u.getUserId());
		model.set("updator_name", u.getUserName());
		return model;
	}

	protected Map setOperator(Map model, Users u) {
		model.put("update_time", new Date());
		model.put("updator_id", u.getUserId());
		model.put("updator_name", u.getUserName());
		return model;
	}
	
	
	
	/*@Override
	public List<UploadFile> getFiles() {
		// TODO Auto-generated method stub
		return super.getFiles();
	}*/

	public List<String> uploads(String fieldName){
		SimpleDateFormat year = new SimpleDateFormat("yyyy");//获取当前系统年份
		SimpleDateFormat month = new SimpleDateFormat("MM");//获取当前系统月份
		Date curDate = new Date(System.currentTimeMillis());

		String str = year.format(curDate) + File.separator + month.format(curDate);

		String savePath = UploadUtils.creatSavePath(getRequest(), str, "upload");
		
		
		List<UploadFile> fileList = getFiles(savePath);
		if(fileList != null && !fileList.isEmpty()) {
			List<String> urls = new ArrayList<String>();
			
			for(UploadFile file : fileList) {
				if (file != null) {
					File f2 = file.getFile();//得到文件保存的完整路径
					File f = new File(f2.toString());
					String newName = "";
					if (f.exists()) {
						String name = f.getName();
						newName = UploadUtils.creatSaveFileName(name);//获取新的名称
						File df = new File(savePath, newName);
						f.renameTo(df);

					}

					String retUrl = (str + "/" + newName).replace(File.separator, "/");
					System.out.println("retUrl=:" + retUrl);
					urls.add(retUrl);
				}
			}
			return urls;
		}
		return null;
		
	}
	
	

	public String upload(String fieldName) {
		SimpleDateFormat year = new SimpleDateFormat("yyyy");//获取当前系统年份
		SimpleDateFormat month = new SimpleDateFormat("MM");//获取当前系统月份
		Date curDate = new Date(System.currentTimeMillis());

		String str = year.format(curDate) + File.separator + month.format(curDate);

		String savePath = UploadUtils.creatSavePath(getRequest(), str, "upload");
				System.out.println("savePath=:"+savePath);
		UploadFile file = getFile(fieldName, savePath);
		if (file != null) {
			//		String ss = getPara("CKEditor");
			//		System.out.println("ss=:"+ss);
			/*String f1 = file.getContentType();//得到上传文件的类型
			String f3 = file.getOriginalFileName();//得到文件原始名称
			String f4 = file.getParameterName();//得到上传的参数，此处为upload
			String f5 = file.getSaveDirectory();//得到文件的保存路径
			String f6 = file.getFileName();//得到文件保存后的名称
			*/
			File f2 = file.getFile();//得到文件保存的完整路径
			//		System.out.println("f2=:"+f2);
			File f = new File(f2.toString());
			String newName = "";
			if (f.exists()) {
				String name = f.getName();
				newName = UploadUtils.creatSaveFileName(name);//获取新的名称
				File df = new File(savePath, newName);
				f.renameTo(df);

			}

			//String url = UploadUtils.creatHeaderUrl(getRequest());
			String retUrl = (str + "/" + newName).replace(File.separator, "/");
			System.out.println("retUrl=:" + retUrl);

			return retUrl;
		}
		return "";
	}
	
	public String getUUID(){
		return UUID.randomUUID().toString().replaceAll("-", "");
	}
	
	public Map<String,Object> getParams(Map<String,String[]> params){
		Map<String,Object> result = new HashMap<String, Object>();
		if(params != null && params.size() > 0) {
			for(Entry<String, String[]> entry : params.entrySet()) {
				String key = entry.getKey();
				String[] val = entry.getValue();
				if(val.length > 0) {
					result.put(key, val[0]);
				}else {
					result.compute(key, null);
				}
			}
		}
		return result;
	}

}